package org.github.manx98.jlibhac.common.keys;

import cn.hutool.core.util.HexUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @author Manx98
 * @version 1.0
 * @since 2022/10/3 0:04
 */
@Slf4j
public class KeyInfo {
    public final String Name;
    public final KeyGetter Getter;
    public final int Group;
    public final KeyRangeType RangeType;
    public final KeyType Type;
    public final int RangeStart;
    public final int RangeEnd;

    public KeyInfo(int group, KeyType type, String name, KeyGetter retrieveFunc) {
        Name = name;
        RangeType = KeyRangeType.Single;
        Type = type;
        RangeStart = 0;
        RangeEnd = 0;
        Group = group;
        Getter = retrieveFunc;
    }

    public KeyInfo(int group, KeyType type, String name, int rangeStart, int rangeEnd, KeyGetter retrieveFunc) {
        Name = name;
        RangeType = KeyRangeType.Range;
        Type = type;
        RangeStart = rangeStart;
        RangeEnd = rangeEnd;
        Group = group;
        Getter = retrieveFunc;
    }

    public int matches(String keyName) {
        if (RangeType == KeyRangeType.Single) {
            if (Name.equals(keyName)) {
                return 1;
            }
        } else {
            int i = keyName.length() - 3;
            if (keyName.charAt(i) == '_') {
                String s = keyName.substring(i + 1);
                if (keyName.equals(Name + "_" + s)) {
                    return HexUtil.decodeHex(s)[0];
                }
            }
        }
        return -1;
    }
}
